package com.content.scaffold.common.client.proxy;


import com.content.scaffold.common.utils.RedisUtils;
import lombok.Data;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

/**
 * @author xiaojl
 */
@Data
public class NotaryCloudClientAdvise implements MethodInterceptor {

    private RedisUtils redisUtils;


    private static NotaryCloudRequestAdvise advise;

    private static NotaryCloudRequestPointCut pointCut;

    private static Advisor advisor;

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Object retVal = methodInvocation.proceed();
        ProxyFactory cloudClientProxy = new ProxyFactory();
        cloudClientProxy.setTarget(retVal);
        if (null == advise) {
            advise = new NotaryCloudRequestAdvise();
            advise.setRedisUtils(redisUtils);
        }
        if (null == pointCut) {
            pointCut = new NotaryCloudRequestPointCut();
        }
        if (null == advisor) {
            advisor = new DefaultPointcutAdvisor(pointCut, advise);
        }
        cloudClientProxy.addAdvisor(advisor);
        return cloudClientProxy.getProxy();
    }

}
